<?php

class Match extends Eloquent {
	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'match_data';

	/**
	 * Fetches the match type
	 *
	 * @return MatchType 
	 */
	public function type()
    {
        return $this->hasOne('MatchType', 'id', 'matchTypeId');
    }
	
	/**
	 * Fetches the field where the match is played
	 *
	 * @return Field 
	 */
	public function field()
    {
        return $this->hasOne('Field', 'id', 'fieldId');
    }
	
	/**
	 * Fetches the admin user of the match
	 *
	 * @return User 
	 */
	public function admin()
    {
        return $this->hasOne('User', 'id', 'admin_userId');
    }

    /**
	 * Fetches all the teams that are playing the match 
	 *
	 * @return Team array
	 */
	public function teams()
    {
        return $this->belongsToMany('Team', 'match_teams', 'matchId', 'teamId');
    }
	
	/**
	 * Fetches all the users that are substitutes of the match 
	 *
	 * @return User array
	 */
	public function substitutes()
    {
        return $this->belongsToMany('User', 'match_subs', 'matchId', 'userId');
    }
	
	/**
	 * Fetches all the users that are guests of the match 
	 *
	 * @return User array
	 */
	public function guests()
    {
        return $this->belongsToMany('User', 'match_guests', 'matchId', 'userId')->withPivot('confirmed');
    }
}
